#!/usr/bin/python
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""GIT callback handler

For Git callback page handler.
"""

__author__ = 'Xin Zhou(zhounew@google.com)'

from git.logic import git_callback_action
from git.logic import git_callback_logic
from git.logic import git_callback_request
from git.logic import git_callback_response
from git.util import git_context

class GitCallbackHandler(object):
  def __init__(self, email, purpose, url, idp_post):
    self.email_ = email
    self.purpose_ = purpose
    self.url_ = url
    self.idp_post_ = idp_post
  
  def Execute(self):
    api_client = git_context.GitContext.GetApiClient()
    assertion = api_client.verify(self.url_, self.idp_post_)
    req = git_callback_request.GitCallbackRequest(self.email_, self.purpose_,
                                                  assertion)
    res = git_callback_response.GitCallbackResponse()
    action = git_callback_action.GitCallbackAction()
    logic = git_callback_logic.GitCallbackLogic(action)
    logic.Run(req, res)
    error = res.GetError()
    if error:
      return error
    else:
      return res.GetOutput()